package spade;

import java.util.*;

public class ACLMessage
{
	final String	ACCEPT_PROPOSAL	  = "accept-proposal";
	final String	AGREE	          = "agree";
	final String	CANCEL	          = "cancel";
	final String	CFP	              = "cfp";
	final String	CALL_FOR_PROPOSAL	= "call-for-proposal";
	final String	CONFIRM	          = "confirm";
	final String	DISCONFIRM	      = "disconfirm";
	final String	FAILURE	          = "failure";
	final String	INFORM	          = "inform";
	final String	NOT_UNDERSTOOD	  = "not-understood";
	final String	PROPOSE	          = "propose";
	final String	QUERY_IF	      = "query-if";
	final String	QUERY_REF	      = "query-ref";
	final String	REFUSE	          = "refuse";
	final String	REJECT_PROPOSAL	  = "reject-proposal";
	final String	REQUEST	          = "request";
	final String	REQUEST_WHEN	  = "request-when";
	final String	REQUEST_WHENEVER  = "request-whenever";
	final String	SUBSCRIBE	      = "subscribe";
	final String	INFORM_IF	      = "inform-if";
	final String	PROXY	          = "proxy";
	final String	PROPAGATE	      = "propagate";

	String	     cid_base	          = "";
	static int	 cid_autocount	      = 0;
	String[]	 commacts	          =
	                                          { "accept-proposal", "agree",
	        "cancel", "cfp", "call-for-proposal", "confirm", "disconfirm",
	        "failure", "inform", "not-understood", "propose", "query-if",
	        "query-ref", "refuse", "reject-proposal", "request",
	        "request-when", "request-whenever", "subscribe", "inform-if",
	        "proxy", "propagate"	          };
	// Map<String, Object> attrs = new HashMap<String, Object>();
	Vector<AID>	 receivers	          = new Vector<AID>();
	AID	         sender	              = null;
	String	     content	          = null;
	String	     performative	      = null;
	String	     id	                  = null;
	Vector<AID>	 reply_to	          = new Vector<AID>();
	String	     reply_with	          = null;
	String	     in_reply_to	      = null;
	String	     encoding	          = null;
	String	     language	          = null;
	String	     ontology	          = null;
	String	     reply_by	          = null;
	String	     protocol	          = null;

	public ACLMessage(String performative)
	{
		init(performative);
	}
	public ACLMessage()
	{
		this(null);
	}

	private void Set_cid_base()
	{
		Random generator = new Random();
		for (int i = 0; i < 4; i++)
		{
			int ran = generator.nextInt(24);
			char c = (char) ran;
			cid_base += c;
		}
	}

	private void init(String performative)
	{
		Set_cid_base();
		if (performative != null
		        && Util.exists(commacts, performative.toLowerCase()))
		{
			this.performative = performative.toLowerCase();
		}

		String ID = this.cid_base + cid_autocount++;
		this.id = ID;

	}

	public void reset()
	{
		init(null);
	}
	public AID getSender()
	{
		return sender;
	}
	public void setSender(AID sender)
	{
		this.sender = sender;
	}
	public void addReceiver(AID recv)
	{
		if (this.receivers.contains(recv) == false)
			this.receivers.add(recv);
	}
	public void removeReceiver(String recv)
	{
		this.receivers.remove(recv);
	}
	public Vector<AID> getReceivers()
	{
		return this.receivers;
	}
	public void addReplyTo(AID aid)
	{
		if (this.reply_to.contains(aid) == false)
			this.reply_to.add(aid);
	}
	public void removeReplyTo(AID aid)
	{
		this.reply_to.remove(aid);
	}
	public Vector<AID> getReplyTo()
	{
		return this.reply_to;
	}
	public void setPerformative(String performative)
	{
		if (Util.exists(this.commacts, performative) == true)
			this.performative = performative;
	}
	public String getPerformative()
	{
		return this.performative;
	}
	public void setContent(String c)
	{
		this.content = c;
	}
	public String getContent()
	{
		return this.content;
	}
	public void setReplyWith(String rw)
	{
		this.reply_with = rw;
	}
	public String getReplyWith()
	{
		return this.reply_with;
	}
	public void setInReplyTo(String reply)
	{
		this.in_reply_to = reply;
	}
	public String getInReplyTo()
	{
		return this.in_reply_to;
	}
	public void setEncoding(String e)
	{
		this.encoding = e;
	}
	public String getEncoding()
	{
		return this.encoding;
	}
	public void setLanguage(String l)
	{
		this.language = l;
	}
	public String getLanguage()
	{
		return this.language;
	}
	public void setOntology(String o)
	{
		this.ontology = o;
	}
	public String getOntology()
	{
		return this.ontology;
	}
	public void setReplyBy(String rb)
	{
		this.reply_by = rb;
	}
	public String getReplyBy()
	{
		return this.reply_by;
	}
	public void setProtocol(String p)
	{
		this.protocol = p;
	}
	public String getProtocol()
	{
		return this.protocol;
	}
	public void setConversationId(String id)
	{
		this.id = id;
	}
	public String getConversationId()
	{
		return this.id;
	}
	public ACLMessage createReply()
	{
		ACLMessage m = new ACLMessage();
		m.setPerformative(this.performative);
		m.setLanguage(this.language);
		m.setOntology(this.ontology);
		m.setProtocol(this.protocol);
		m.setConversationId(this.id);
		int i;
		for (i = 0; i < this.reply_to.size(); i++)
			m.addReceiver(this.reply_to.get(i));

		if (this.reply_to.isEmpty())
			m.addReceiver(this.sender);

		m.setInReplyTo(this.reply_with);
		if (this.reply_with == null)
			m.setConversationId(this.reply_with);

		return null;
	}

}
